<?php
/**
 * Created by PhpStorm.
 * User: dubin
 * Date: 2016/12/19
 * Time: 20:09
 */
namespace App\Repositories\Eloquent;
use App\Repositories\Eloquent\Repository;
use App\Models\Menu;

class MenuRepository extends Repository{
    
    public function model()
    {
        return Menu::class;
    }

    /**
     * 数据递归
     * @param $menus
     * @param int $pid
     * @return string
     */
    public function sortMenu($menus,$pid=0)
    {
        $arr=[];
        if(empty($menus))
        {
            return '';
        }
        foreach ($menus as $k=>$v)
        {
            if ($v['parent_id'] == $pid)
            {
                $arr[$k]=$v;
                $arr[$k]['child']=self::sortMenu($menus,$v['id']);
            }
        }
        return $arr;
    }

    /**
     * 排序子菜单顺序
     * @param string $value
     */
    public function sortMenuSetCache($menus)
    {
        foreach ($menus as $k=>&$v)
        {
            if($v['child'])
            {
                $sort=array_column($v['child'],'sort');
                array_multisort($sort,SORT_DESC,$v['child']);
            }
        }

        return $menus;
    }
    
}